/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.commands.Delegate;
import com.bourre.data.collections.Map;
import com.bourre.transitions.FPSBeacon;
import com.bourre.transitions.IFrameListener;

import fever.events.FrameEvent;
import fever.log.FeverDebug;
import fever.utils.Stringifier;

import flash.display.BitmapData;
import flash.geom.Rectangle;

/**
 * Use <strong>BitmapData</strong> as source and control it like a Timeline animated 
 * MovieClip.
 * 
 * @author Romain Ecarnot
 */
class fever.display.BitmapClip implements IFrameListener
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _source : BitmapData;
	private var _cellWidth : Number;
	private var _cellHeight : Number;
	
	private var _currentFrame : Number;
	private var _maxFrames : Number;
	private var _isPlaying : Boolean;
	private var _bitmap : BitmapData;
	private var _track : MovieClip;
	
	private var _listeners : Map;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * @param target MovieClip target where animation track will be created
	 * @param data BitmapData content source
	 * @param nbImage Animation image length
	 */ 
	public function BitmapClip( target : MovieClip, data : BitmapData, nbImage : Number ) 
	{
		_source = data;
		_cellWidth = data.width / nbImage;
		_cellHeight = data.height;
		
		_bitmap = new BitmapData( _cellWidth, _cellHeight, true, 0xffffff );
		_maxFrames = ( _source.width / _cellWidth );
		_isPlaying = false;
		_currentFrame = 1;
		
		_setAnimationTarget( target );
		
		_listeners = new Map();
	}
	
	/**
	 * Returns current drawing target.
	 */
	public function getView( ) : MovieClip
	{
		return _track;
	}
	
	/**
	 * Shows content.
	 */
	public function show(  ) : Void
	{
		_track._visible = true;
	}
	
	/**
	 * Hides content.
	 */
	public function hide(  ) : Void
	{
		_track._visible = false;
	}
	
	/**
	 * Returns {@code true} if clip is currently playing.
	 */
	public function isPlaying(  ) : Boolean
	{
		return _isPlaying;
	}
	
	/**
	 * Adds a script call for frame {@code index}.
	 * 
	 * <p>Auto delegation is used ( Pixlib implementation )
	 */
	public function addFrameScript( index : Number, scope ) : Void
	{
		if( !_isValidIndex( index ) ) return;
		
		var f : Function = arguments[ 2 ];
		if ( f ) scope = _getEventProxy.apply( this, arguments.splice( 1 ) );
		
		if( !_listeners.containsKey( index ) ) 
			_listeners.put( index, scope );
		else 
			FeverDebug.WARN( 'Frame ' + index + ' is already under script control' );
	}
	
	/**
	 * Removes script for passed-in frame {@code index}.
	 */
	public function removeFrameScript( index : Number ) : Void
	{
		_listeners.remove( index );
	}
	
	/**
	 * Adds a stop command to passed-in frame {@code index}.
	 */
	public function addFrameStop( index : Number ) : Void
	{
		addFrameScript( index, this, _stop );
	}
	
	/**
	 * Plays animation
	 */
	public function play( ) : Void
	{
		if( !_isPlaying )
		{
			_isPlaying = true;
			FPSBeacon.getInstance().addFrameListener( this );
		}
	}
	
	/**
	 * Stops animation
	 */
	public function stop(  ) : Void
	{
		if( _isPlaying )
		{
			_isPlaying = false;
			FPSBeacon.getInstance().removeFrameListener( this );
		}
	}
	
	/**
	 * Stops animation and go to frame {@code index}
	 */
	public function gotoAndStop( index : Number ) : Void
	{
		stop();
		
		if( _isValidIndex( index ) ) _updateClipFrame( index );
	}
	
	/**
	 * Play animation from {@code index} frame. 
	 */
	public function gotoAndPlay( index : Number ) : Void
	{
		if( _isValidIndex( index ) )
		{
			_currentFrame = index;
		
			if( !_isPlaying ) play();
		}
	} 
	
	/** 
	 * Triggers at each displayed frame by the player.
	 */
	public function onEnterFrame() : Void
	{
		_updateClipFrame( _getNextFrame() );
	}
	
	/**
	 * Go to next frame.
	 */
	public function nextFrame(  ) : Void
	{
		if( !_isPlaying ) _updateClipFrame( _getNextFrame() );
	}
	
	/**
	 * Go to previous frame.
	 */
	public function prevFrame(  ) : Void
	{
		if( !_isPlaying ) _updateClipFrame( _getPreviousFrame() );	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _setAnimationTarget( mc : MovieClip ) : Void 
	{
		if( _track ) _track.removeMovieClip();
		
		_track = mc;
		_track.attachBitmap( _bitmap, 1 );
	}
	
	private function _isValidIndex( n : Number ) : Boolean
	{
		return ( _maxFrames >= n && 1 <= n);
	}
	
	private function _updateClipFrame( index : Number ) : Void
	{
		_bitmap.copyPixels( 
			_source, 
			new Rectangle( ( index - 1 ) * _cellWidth, 0, _cellWidth, _cellHeight ), 
			new flash.geom.Point( 0, 0 ) 
		);
		
		_track._currentframe = index;
		
		if( _listeners.containsKey( index ) )
		{
			_fireEvent( 
				_listeners.get( index ), 
				new FrameEvent( FrameEvent.onFrameEVENT, this, index ) 
			);	
		}
	}
	
	private function _getNextFrame() : Number 
	{
		if( _currentFrame + 1 > _maxFrames ) _currentFrame = 1;
		else _currentFrame++;
		
		return _currentFrame;
	}
	
	private function _getPreviousFrame(  ) : Number 
	{
		if( _currentFrame -1 < 1 ) _currentFrame = _maxFrames;
		else _currentFrame--;
		
		return _currentFrame;
	}
	
	/**
	 * Uses Pixlib EventBroadcaster#_getEventProxy implementation
	 */
	private function _getEventProxy( scope, method : Function ) : Function
	{
		return Delegate.create.apply( Delegate, [ scope, method ].concat( arguments.splice( 2 ) ) );
	}
	
	/**
	 * Uses Pixlib EventBroadcaster#_broadcast implementation
	 */
	private function _fireEvent( o, e : FrameEvent ) : Void
	{
		var s : String = typeof( o );
		
		if ( s == 'object' || s == 'movieclip' ) 
		{
			if ( o.handleEvent != undefined ) o.handleEvent( e );
			else o[ String( e.getType() ) ]( e );
		}
		else o.apply( this, [e] );
	}
	
	private function _stop( event : FrameEvent ) : Void
	{
		BitmapClip( event.getTarget() ).stop();	
	}
}